<?php
	/**
	 * This class will have all the required functions that can deal with doc, docx and pdf
	 * @author Intellact
	 *
	 */
	class parser
	{
		protected  $current_file_name, $current_file_ext, $current_file_data, $current_file_path, $error;
		protected $emails = array();
		
		/**
		 * This function will parse the test form $this->current_file_name
		 * @author: Intellact
		 * @return: null
		 */
		public function parseText()
		{
			$userDoc = $this->current_file_path . $this->current_file_name;
			$fileHandle = fopen($userDoc, "r");
			$word_text = @fread($fileHandle, filesize($userDoc));
		
			$line = "";
			$tam = filesize($userDoc);
			$caracteres = $nulos = 0;
		
			for($i=1536; $i<$tam; $i++)
			{
				$line .= $word_text[$i];
		
				if( $word_text[$i] == 0)
					$nulos++;
				else
				{
					$nulos=0;
					$caracteres++;
				}
		
				if( $nulos>1996)
					break;
			}
		
			//echo $caracteres;

			$lines = explode(chr(0x0D),$line);
			
			$outtext = "";
			foreach($lines as $thisline)
			{
				$tam = strlen($thisline);
				if( !$tam )
					continue;
	
				$new_line = "";
				for($i=0; $i<$tam; $i++)
				{
					$onechar = $thisline[$i];
					if( $onechar > chr(240) )
						continue;
	
					if( $onechar >= chr(0x20) )
					{
						$caracteres++;
						$new_line .= $onechar;
					}
	
					( $onechar == chr(0x14) ) ? ($new_line .= "</a>") : (true);
	
					if( $onechar == chr(0x07) )
					{
						$new_line .= "\t";
						if( isset($thisline[$i+1]) )
							if( $thisline[$i+1] == chr(0x07) )
								$new_line .= "\n";
					}
				}
				//troca por hiperlink
				$new_line = str_replace("HYPERLINK" ,"<a href=",$new_line);
				$new_line = str_replace("\o" ,">",$new_line);
				$new_line .= "\n";
	
				//link de imagens
				$new_line = str_replace("INCLUDEPICTURE" ,"<br><img src=",$new_line);
				$new_line = str_replace("\*" ,"><br>",$new_line);
				$new_line = str_replace("MERGEFORMATINET" ,"",$new_line);
	
				$this->current_file_data .= nl2br($new_line);
			}
		}

		/**
		* This function will store all the emails available in $this->current_file_data
		* @author: Intellact
		* @return: null
		*/
		public function getEmailFromData()
		{
			if ($this->current_file_data != '')
			{
				$res = preg_match_all(VALID_EMAIL_REGEX, $this->current_file_data, $matches);
				
				if ($res)
					foreach(array_unique($matches[0]) as $email)
						$this->emails[] = $email;
			}
		}
		
		public function getAllEmils()
		{
			return $this->emails;
		}
	}
?>